// Eric McDonald
// Tic Tac Tioe with hopefully working AI

#include <iostream>
using namespace std;

char boardPosition1 = '1';
char boardPosition2 = '2';
char boardPosition3 = '3';
char boardPosition4 = '4';
char boardPosition5 = '5';
char boardPosition6 = '6';
char boardPosition7 = '7';
char boardPosition8 = '8';
char boardPosition9 = '9';
char mark;
int pTurn = 1;


class Board
{
public:
	void drawBoard ()
	{
	cout << "  " << boardPosition1 << "  |  " << boardPosition2 << "  |  " << boardPosition3 << endl;
	cout << "_________________" << endl;
	cout << "  " << boardPosition4 << "  |  " << boardPosition5 << "  |  " << boardPosition6 << endl;
	cout << "_________________" << endl;
	cout << "  " << boardPosition7 << "  |  " << boardPosition8 << "  |  " << boardPosition9 << endl;
	}
};

class secPlayer
{
public:
	int pMove2 (int pTurn)
	{

		if (boardPosition1 != '1')
		{
			if (boardPosition2 == boardPosition1 && boardPosition3 == '3')
			{
				boardPosition3 = 'O';
			}
			if (boardPosition3 == boardPosition1 && boardPosition2 == '2')
			{
				boardPosition2 = 'O';
			}
			if (boardPosition4 == boardPosition1 && boardPosition7 == '7')
			{
				boardPosition7 = 'O';
			}
			if (boardPosition7 == boardPosition1 && boardPosition4 =='4')
			{
				boardPosition4 = 'O';
			}
			if (boardPosition5 == boardPosition1 && boardPosition9 == '9')
			{
				boardPosition9 = 'O';
			}
			if (boardPosition9 == boardPosition1 && boardPosition5 == '5')
			{
				boardPosition5 = 'O';
			}
			cout << "  " << boardPosition1 << "  |  " << boardPosition2 << "  |  " << boardPosition3 << endl;
			cout << "_________________" << endl;
			cout << "  " << boardPosition4 << "  |  " << boardPosition5 << "  |  " << boardPosition6 << endl;
			cout << "_________________" << endl;
			cout << "  " << boardPosition7 << "  |  " << boardPosition8 << "  |  " << boardPosition9 << endl;
		}
		return (pTurn = 1);
	}
	
};




int main()
{
	
	bool valid;
	bool GameOver = false;
	bool GameWin = true;
	char check;
	Board TicTacToeBoard;
	secPlayer Player2;

	TicTacToeBoard.drawBoard();

	do
	{

		if (pTurn = 2)
		{
			Player2.pMove2(pTurn);
		}
		


		if (pTurn == 1)
		{	
			mark = 'X';
		}
		

		

		cout << "Your move, Meatbag" << endl;

		do 
		{
			char pMove;
			cin >> pMove;
			valid = true;

			

		
			if (pMove == '1' && boardPosition1 == '1')
			{
				boardPosition1 = mark;
			} 
			else if (pMove == '2' && boardPosition2 == '2')
			{
				boardPosition2 = mark;
			} 
			else if (pMove == '3' && boardPosition3 == '3')
			{
				boardPosition3 = mark;
			} 
			else if (pMove == '4' && boardPosition4 == '4')
			{
				boardPosition4 = mark;
			}
			else if (pMove == '5' && boardPosition5 == '5') 
			{
				boardPosition5 = mark;
			} 
			else if (pMove == '6' && boardPosition6 == '6') 
			{
				boardPosition6 = mark;
			} 
			else if (pMove == '7' && boardPosition7 == '7') 
			{
				boardPosition7 = mark;
			} 
			else if (pMove == '8' && boardPosition8 == '8') 
			{
				boardPosition8 = mark;
			}
			else if (pMove == '9' && boardPosition9 == '9') 
			{
				boardPosition9 = mark;
			} 
			else 
			{
				std::cout << "Invalid Move. Try again." << std::endl;
				valid = false;
			}
		}while (!valid);

			
		

		GameOver = false;
		GameWin = true;

		if (boardPosition1 != '1')
		{
			if (boardPosition2 == boardPosition1 && boardPosition3 == boardPosition1)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition1 && boardPosition7 == boardPosition1)
			{
				GameOver = true;
			}

		}

		if (boardPosition5 != '5')
		{
			if (boardPosition1 == boardPosition5 && boardPosition9 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition2 == boardPosition5 && boardPosition8 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition4 == boardPosition5 && boardPosition6 == boardPosition5)
			{
				GameOver = true;
			}
			if (boardPosition3 == boardPosition5 && boardPosition7 == boardPosition5)
			{
				GameOver = true;
			}
		}

		if (boardPosition9 != '9')
		{
			if (boardPosition3 == boardPosition9 && boardPosition6 == boardPosition9)
			{
				GameOver = true;
			}
			if (boardPosition7 == boardPosition9 && boardPosition8 == boardPosition9)
			{
				GameOver = true;
			}
		}

		if (boardPosition1 != '1' && boardPosition2 != '2' && boardPosition3 != '3' &&
			boardPosition4 != '4' && boardPosition5 != '5' && boardPosition6 != '6' &&
			boardPosition7 != '7' && boardPosition8 != '8' && boardPosition9 != '9')
		{
			GameOver = true;
			GameWin = false;
		}


		if (GameOver)
		{
			if (GameWin)
			{
				cout << "You win this round, Meatbag...." << endl;
			}

			TicTacToeBoard.drawBoard();

			cout << "It seems we've filled the board. How humiliating." << endl;
			cout << "Are you willing to attempt again, Meatbag?" << endl;
			cin >> check;

			if (check == 'y')
			{
				GameOver = false;

				char boardPosition1 = '1';
				char boardPosition2 = '2';
				char boardPosition3 = '3';
				char boardPosition4 = '4';
				char boardPosition5 = '5';
				char boardPosition6 = '6';
				char boardPosition7 = '7';
				char boardPosition8 = '8';
				char boardPosition9 = '9';
			}
			pTurn = 1;
		}
		else
		{
			if (pTurn == 1)
			{
				pTurn = 2;

			}
			else 
			{
				pTurn = 1;
			}
		}

	} while(!GameOver);

return (0);
}



